require 'drb/drb'
require 'milfs/index'
require 'pp'

module Mil
  module Service
    class Index
      include DRb::DRbUndumped

      def initialize(path)
	@index = Mil::Index::Index.new :database => path
	@search = Mil::Index::Search.new :database => path
      end

      def is_namespace?(namespace)
	@search.open
	@search.terms.include? "namespace:#{namespace}"
      end

      def namespaces
	@search.open
	namespaces = @search.terms.collect { |t|
	  if t =~ /^namespace:/
	    t
	  end
	}

	unless namespaces.include? "global"
	  namespaces << "global"
	end

	namespaces
      end

      def index(file)
	@index.open
	doc = @index.generate_doc(file.node, file.name, file.namespaces,  file.tags)
	@index.save(doc)
      end

      def query(query, max=50)
	@search.open
	@search.query = query

	@search.results(0,max.to_i)
      end
    end
  end
end
